1
|
|
|
var Subject = require('./Subject'), |
2
|
|
|
Fact = require('./Fact'), |
3
|
|
|
ResourceReference = require('./ResourceReference'), |
4
|
|
|
utils = require('./utils'); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A relationship. |
8
|
|
|
* |
9
|
|
|
* @constructor |
10
|
|
|
* @param {Object} [json] |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
var Relationship = function(json){ |
13
|
|
|
|
14
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
15
|
|
|
if(!(this instanceof Relationship)){ |
16
|
|
|
return new Relationship(json); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
20
|
|
|
if(Relationship.isInstance(json)){ |
21
|
|
|
return json; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
Subject.call(this, json); |
25
|
|
|
|
26
|
|
|
if(json){ |
|
|
|
|
27
|
|
|
this.setType(json.type); |
28
|
|
|
this.setPerson1(json.person1); |
29
|
|
|
this.setPerson2(json.person2); |
30
|
|
|
this.setFacts(json.facts); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
}; |
33
|
|
|
|
34
|
|
|
Relationship.prototype = Object.create(Subject.prototype); |
35
|
|
|
|
36
|
|
|
Relationship._gedxClass = Relationship.prototype._gedxClass = 'GedcomX.Relationship'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check whether the given object is an instance of this class. |
40
|
|
|
* |
41
|
|
|
* @param {Object} obj |
42
|
|
|
* @returns {Boolean} |
43
|
|
|
*/ |
44
|
|
|
Relationship.isInstance = function(obj){ |
45
|
|
|
return utils.isInstance(obj, this._gedxClass); |
46
|
|
|
}; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the relationship type |
50
|
|
|
* |
51
|
|
|
* @returns {String} type |
52
|
|
|
*/ |
53
|
|
|
Relationship.prototype.getType = function(){ |
54
|
|
|
return this.type; |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set the relationship type |
59
|
|
|
* |
60
|
|
|
* @param {String} type |
61
|
|
|
* @returns {Relationship} This instance |
62
|
|
|
*/ |
63
|
|
|
Relationship.prototype.setType = function(type){ |
64
|
|
|
this.type = type; |
65
|
|
|
return this; |
66
|
|
|
}; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get person1 reference |
70
|
|
|
* |
71
|
|
|
* @returns {ResourceReference} |
72
|
|
|
*/ |
73
|
|
|
Relationship.prototype.getPerson1 = function(){ |
74
|
|
|
return this.person1; |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the person1 reference |
79
|
|
|
* |
80
|
|
|
* @param {ResourceReference} person1 |
81
|
|
|
* @returns {Relationship} This instance |
82
|
|
|
*/ |
83
|
|
|
Relationship.prototype.setPerson1 = function(person1){ |
84
|
|
|
if(person1){ |
85
|
|
|
this.person1 = ResourceReference(person1); |
86
|
|
|
} |
87
|
|
|
return this; |
88
|
|
|
}; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get person2 reference |
92
|
|
|
* |
93
|
|
|
* @returns {ResourceReference} |
94
|
|
|
*/ |
95
|
|
|
Relationship.prototype.getPerson2 = function(){ |
96
|
|
|
return this.person2; |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the person1 reference |
101
|
|
|
* |
102
|
|
|
* @param {ResourceReference} person2 |
103
|
|
|
* @returns {Relationship} This instance |
104
|
|
|
*/ |
105
|
|
|
Relationship.prototype.setPerson2 = function(person2){ |
106
|
|
|
if(person2){ |
107
|
|
|
this.person2 = ResourceReference(person2); |
108
|
|
|
} |
109
|
|
|
return this; |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the facts |
114
|
|
|
* |
115
|
|
|
* @return {Fact[]} |
116
|
|
|
*/ |
117
|
|
|
Relationship.prototype.getFacts = function(){ |
118
|
|
|
return this.facts || []; |
119
|
|
|
}; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the facts |
123
|
|
|
* |
124
|
|
|
* @param {Fact[]|Object[]} facts |
125
|
|
|
* @returns {Relationship} This instance |
126
|
|
|
*/ |
127
|
|
|
Relationship.prototype.setFacts = function(facts){ |
128
|
|
|
return this._setArray(facts, 'facts', 'addFact'); |
129
|
|
|
}; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add a fact |
133
|
|
|
* |
134
|
|
|
* @param {Fact|Object} fact |
135
|
|
|
* @returns {Relationship} This instance |
136
|
|
|
*/ |
137
|
|
|
Relationship.prototype.addFact = function(fact){ |
138
|
|
|
return this._arrayPush(fact, 'facts', Fact); |
139
|
|
|
}; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Export the object as JSON |
143
|
|
|
* |
144
|
|
|
* @return {Object} JSON object |
145
|
|
|
*/ |
146
|
|
|
Relationship.prototype.toJSON = function(){ |
147
|
|
|
return this._toJSON(Subject, [ |
148
|
|
|
'type', |
149
|
|
|
'person1', |
150
|
|
|
'person2', |
151
|
|
|
'facts' |
152
|
|
|
]); |
153
|
|
|
}; |
154
|
|
|
|
155
|
|
|
module.exports = Relationship; |